package netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

import java.nio.charset.StandardCharsets;

/**
 * @Auther DRevelation
 * @Date 2019-08-14 16:05
 * @Desc
 */
public class NettyServer {
    public static void main(String[] args) {
        ServerBootstrap serverBootstrap = new ServerBootstrap();

        EventLoopGroup boos = new NioEventLoopGroup();
        EventLoopGroup worker = new NioEventLoopGroup();

        serverBootstrap.group(boos, worker)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
                        ch.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                ctx.channel().eventLoop().submit(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            Thread.sleep(10 * 1000);
                                            System.out.println("======汪汪汪======");
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                                System.out.println("channelActive=" + Unpooled.wrappedBuffer("喵喵喵~".getBytes(StandardCharsets.UTF_8)));
                                super.channelActive(ctx);
                            }

                            @Override
                            public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("channelReadComplete=" + Unpooled.wrappedBuffer("喵喵喵2~".getBytes(StandardCharsets.UTF_8)));
                                super.channelReadComplete(ctx);
                            }

                            @Override
                            protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {
                                System.out.println(s);
                            }
                        });
                    }
                })
                .bind(8000);
    }
}
